package org.lwz.space.service.impl;

import org.lwz.space.dao.UserDao;
import org.lwz.space.model.User;
import org.lwz.space.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Liaowz on 2015/10/16.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void save(User user) {
        this.userDao.save(user);
    }

    @Override
    public void delete(User user) {
        //TODO: 删除用户之前先查出来， 只用被Hibernate持久化的对象才能被删除
        User persistentUser = userDao.getUser(user.getUserName(), user.getPassword());
        this.userDao.delete(persistentUser);

//        userDao.delete(user); //TODO:错
    }

    @Override
    public List<User> list() {
        return this.userDao.list();
    }

    @Override
    public void update(User user) {
        //TODO: 删除用户之前先查出来， 只用被Hibernate持久化的对象才能被更新
        User persistentUser = userDao.getUser(user.getUserName(), user.getPassword());
        persistentUser.setEmail(user.getEmail());
//        设置其他属性等等
        userDao.save(persistentUser);

//        this.userDao.update(user); //TODO: 错
    }

    @Override
    public User getUser(String userName, String password) {
        return userDao.getUser(userName, password);
    }


}
